package org.aplikator.client.shared.rpc.marshaller;

import org.aplikator.client.shared.descriptor.PropertyDTO;
import org.aplikator.client.shared.rpc.ParameterWrapperGetPage;
import org.jboss.errai.marshalling.client.api.Marshaller;
import org.jboss.errai.marshalling.client.api.MarshallingSession;
import org.jboss.errai.marshalling.client.api.annotations.ClientMarshaller;
import org.jboss.errai.marshalling.client.api.annotations.ServerMarshaller;
import org.jboss.errai.marshalling.client.api.json.EJObject;
import org.jboss.errai.marshalling.client.api.json.EJValue;
import org.jboss.errai.marshalling.client.marshallers.AbstractNullableMarshaller;

@ClientMarshaller(ParameterWrapperGetPage.class)
@ServerMarshaller(ParameterWrapperGetPage.class)
public class ParameterWrapperGetPageMarshaller extends AbstractNullableMarshaller<ParameterWrapperGetPage> {

	/*@Override
	public Class<ParameterWrapperGetPage> getTypeHandled() {
		return ParameterWrapperGetPage.class;
	}*/

	@Override
	public ParameterWrapperGetPage[] getEmptyArray() {
		return new ParameterWrapperGetPage[0];
	}

	@Override
	public ParameterWrapperGetPage doNotNullDemarshall(EJValue o,
			MarshallingSession ctx) {
		if (o.isObject() != null) {
			EJObject ejObject = o.isObject();
			ParameterWrapperGetPage pg = new ParameterWrapperGetPage();
			
			if (DemarshallingUtils.containsAndNotNull(ejObject, "viewId")) {
				String str = ejObject.get("viewId").isString().stringValue();
				pg.setViewId(str);
			}
			if (DemarshallingUtils.containsAndNotNull(ejObject, "ownerProperty")) {
				Marshaller<Object> marsh = ctx.getMarshallerInstance(PropertyDTO.class.getName());
				Object demarshalled = marsh.demarshall(ejObject.get("ownerProperty"), ctx);
				pg.setOwnerProperty((PropertyDTO) demarshalled);
			}				
			return pg;
		} else return null;
	}

	@Override
	public String doNotNullMarshall(ParameterWrapperGetPage o,
			MarshallingSession ctx) {
		StringBuilder builder = new StringBuilder().append('{');
		MarshallingUtils.string("viewId", o.getViewId(), builder).append(',');
		MarshallingUtils.key("ownerProperty", builder).append(':');
		Marshaller<Object> marsh = ctx.getMarshallerInstance(PropertyDTO.class.getName());
		builder.append(marsh.marshall(o.getOwnerProperty(), ctx));
		builder.append('}');
		return builder.toString();
	}
}
